package com.jic.veteran.controller;

import com.jic.veteran.common.api.CommonPage;
import com.jic.veteran.common.api.CommonResult;
import com.jic.veteran.model.ProjectMessage;
import com.jic.veteran.service.ProjectMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "ProjectMessageController")
@Controller
@RequestMapping("/projectMesage")
public class ProjectMessageController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectMessageController.class);

    @Autowired
    private ProjectMessageService projectMessageService;

    @ApiOperation("根据项目编号获取留言列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<ProjectMessage>> list(@RequestParam(value = "projectId", required = false) String projectId,
                                                         @RequestParam(value = "show", required = false) Boolean show,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<ProjectMessage> projectMessageList = projectMessageService.list(projectId, show, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(projectMessageList));
    }

    @ApiOperation("修改项目留言")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id,
                               @RequestBody ProjectMessage projectMessage) {
        int count = projectMessageService.update(id,projectMessage);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取留言详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ProjectMessage> getItem(@PathVariable Long id) {
        ProjectMessage projectMessage = projectMessageService.getItem(id);
        return CommonResult.success(projectMessage);
    }

    @ApiOperation("根据ID删除留言信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = projectMessageService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("创建留言")
    @RequestMapping(value = "/add/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@PathVariable Long id,@RequestBody ProjectMessage projectMessage) {
        int count = projectMessageService.create(id,projectMessage);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
